import 'package:flutter/material.dart';

import '../../../../utils/util.dart';
import '../../model/page.dart';
import '../state.dart';
import 'sub/sub_controller.dart';

class ITabRefreshState<T> extends IRefreshState {
  final updateHeaderId = 'update-header';

  final updateTabbarId = 'update-tabbar';
  final updateTabBarViewId = 'update-tabbar-view';
  String get updatePageId {
    return getUpdateLayoutPageId(tabCtrl?.index);
  }

  String get updateLayoutPageId {
    return '$updateTabBarViewId-${tabCtrl?.index}';
  }

  String getUpdateLayoutPageId(int? index) {
    return '$updateTabBarViewId-$index';
  }

  @override
  PageModel<T>? get page {
    if (title == null) {
      return null;
    }
    if (data.containsKey(title) == true) {
      return data[title];
    }

    final p = PageModel<T>();
    data[title!] = p;

    return p;
  }

  List<String>? tabTitles;
  late RxInt tabIndex = 0.obs;

  void setupData(List<String>? tabTitles) {
    // tabCtrl?.updateTitles(tabTitles ?? []);
    data = {};
    this.tabTitles = tabTitles;
    tabTitles?.forEach((e) {
      data[e] = PageModel();
    });
  }

  late Map<String, PageModel<T>> data = {};

  String? get title {
    if (tabTitles == null || tabCtrl == null) return null;
    if (tabCtrl!.index >= tabTitles!.length) {
      return null;
    }
    return tabTitles![tabCtrl!.index];
  }

  late Map<String, TabSubController> subCtrls = {};
}
